var _api = require("../../../utils/api"), _config = _interopRequireDefault(require("../../../utils/config")), _cardUtil = _interopRequireDefault(require("../../../utils/card-util")), _serviceUtil = require("../../../utils/service-util"), _contactUtil = require("../../../utils/contact-util"), _radarUtil = require("../../../utils/radar-util"), _withinmsg = _interopRequireDefault(require("../../../public/utils/withinmsg")), _official = _interopRequireDefault(require("./official"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(a) {
    for (var t = 1; t < arguments.length; t++) {
        var e = null != arguments[t] ? arguments[t] : {}, r = Object.keys(e);
        "function" == typeof Object.getOwnPropertySymbols && (r = r.concat(Object.getOwnPropertySymbols(e).filter(function(t) {
            return Object.getOwnPropertyDescriptor(e, t).enumerable;
        }))), r.forEach(function(t) {
            _defineProperty(a, t, e[t]);
        });
    }
    return a;
}

function _defineProperty(t, a, e) {
    return a in t ? Object.defineProperty(t, a, {
        value: e,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = e, t;
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var a = 0, e = new Array(t.length); a < t.length; a++) e[a] = t[a];
        return e;
    }
}

var appData = getApp().globalData;

Page({
    mixins: [ _official.default ],
    data: {
        version: _config.default.appVersion.replace(/\.\d+$/, ""),
        agentModal: !1
    },
    onLoad: function() {
        var t = appData.card, a = appData.contact, e = appData.applyList;
        this.setCardData(t), this.setContactData(a), this.setApplyList(e), this.isManage(), 
        this.isService(), this.initEvents(), this.loadAgent();
    },
    onTabItemTap: function() {
        getApp().reportAnalytics("tabbar_mine"), wx.hideTabBarRedDot({
            index: 3
        });
    },
    initEvents: function() {
        var a = this;
        [ [ "card.change", this.setCardData.bind(this) ], [ "contact.change", this.setContactData.bind(this) ], [ "contact.applylist", this.setApplyList.bind(this) ], [ "service.data.change", this.setServiceData.bind(this) ], [ "change.radar.push", function(t) {
            return a.otherContact.isOpenRadar = t;
        } ], [ "card.avatar.change", function(t) {
            return a.setData({
                "card.avatarThumb": t
            });
        } ] ].forEach(function(t) {
            return a.$addListener.apply(a, _toConsumableArray(t));
        });
    },
    onPullDownRefresh: function() {
        (0, _contactUtil.loadContact)(wx.stopPullDownRefresh), (0, _serviceUtil.loadAccount)();
    },
    setServiceData: function(t) {
        this.$setData({
            service: t && t.id
        });
    },
    setCardData: function(t) {
        this.$setData({
            card: t
        }), (0, _serviceUtil.loadAccount)(), t && t.cardid || this.$route("/pages/tabbar/card/card", "tab");
    },
    setApplyList: function(t) {
        this.$put("contact.applylist", t), this.$setData({
            applyCount: t.length
        });
    },
    otherContact: {},
    setContactData: global.debounce(function(t) {
        var a = t.contact, e = t.contacttotal, r = t.total, i = t.site, n = t.isauthcontact, o = t.isauthradar, s = t.hasminiapp, c = a.expiretime;
        c && (a.expire = c.split(" ")[0]), this.$setData({
            contact: a,
            contacttotal: e,
            total: r,
            site: i
        }), this.otherContact = _objectSpread({}, this.otherContact, {
            isauthcontact: n,
            isauthradar: o,
            hasminiapp: s
        }), (a.isAdmin || a.isCreator) && this.checkIsJoinRadar(this.data.card);
    }, 100, {
        leading: !0,
        trailing: !1
    }),
    checkIsJoinRadar: function() {
        var i = this, n = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : appData.card, o = +n.receivemsg;
        _withinmsg.default.checkExistCorp(n).then(function(t) {
            var a = t.isexist, e = t.issubscribe;
            i.otherContact.isexist = a;
            var r = o === _cardUtil.default.RECEIVEMSG.OPEN;
            o === _cardUtil.default.RECEIVEMSG.NOTSE && (r = n.isFeeVer && a && e), i.otherContact.isOpenRadar = r, 
            i.otherContact.issubscribe = e;
        });
    },
    gotoRoute: function(t) {
        var a = t.currentTarget.dataset.url;
        this.$route(a);
    },
    isManage: function() {
        var a = this;
        _api.api.minicard.god.ishasmanageauth().then(function(t) {
            return t.data.hasauth;
        }).catch(function() {
            return !1;
        }).then(function(t) {
            return a.$setData({
                isManage: t
            });
        });
    },
    isService: function() {
        var r = this;
        _api.json.service().then(function(t) {
            var a = t.uids, e = r.$appData.tokens;
            r.setData({
                isService: (a || []).find(function(t) {
                    return +t == +e.uid;
                })
            });
        });
    },
    loadAgent: function() {
        var e = this;
        _api.api.minicard.user.getuserspreadcounts().then(function(t) {
            var a = t.data.allincome;
            e.setData({
                agentMoney: (a / 100).toFixed(2),
                agentModal: !!a
            });
        });
    },
    onAgentClose: function() {
        this.setData({
            agentModal: !1
        });
    },
    goAgent: function() {
        var t = this;
        wx.navigateToMiniProgram({
            appId: "wx250a54556ac16980",
            success: function() {
                return t.onAgentClose();
            }
        });
    },
    goProvider: function() {
        wx.navigateToMiniProgram({
            appId: "wxecc63b3a901f879c"
        });
    },
    cardEdit: function() {
        this.$report("mine_tap_editcard"), this.$route("/pages/card/edit/edit");
    },
    cardSetting: function() {
        var r = this;
        this.$report("mine_tap_setcard");
        var t = this.data.contact.isFeeVer, i = {
            "欢迎语": "/publicPackage/salesradar/settings/settings",
            "转发标题与卡片": "/pages/card/transferword/transferword",
            "雷达提醒": function() {
                return r.openRadar();
            }
        }, n = [ "autoresponse", "transferword", "radar" ];
        t ? (i["话术库"] = "/publicPackage/salesradar/settings/talklibrary/manage/manage", 
        n.push("talklibrary")) : (i["快捷回复设置"] = "/publicPackage/salesradar/settings/fast-reply/list/list", 
        n.push("fastreply"));
        var o = Object.keys(i);
        wx.showActionSheet({
            itemList: o,
            success: function(t) {
                var a = t.tapIndex, e = i[o[a]];
                r.$report("setcard_tap_".concat(n[a])), "string" == typeof e ? r.$route(e) : e();
            }
        });
    },
    openRadar: function() {
        if (this.data.card.isFeeVer) {
            var t = this.otherContact, a = t.isOpenRadar, e = t.issubscribe;
            this.$route("/pages/radar/open/open?isopen=".concat(a, "&issubscribe=").concat(e));
        } else this.$route("/pages/radar/open/open");
    },
    goVersion: function() {
        this.$report("radar_upgrade_in_mine");
        var t = this.data.contact, a = t.isGroup, e = t.isExpire;
        return a ? this.gotoProvider() : e && appData.isIOS ? this.expireModal() : void (0, 
        _contactUtil.gotoVersion)(this.data.card.guid, "type=contacts", !0);
    },
    expireModal: function() {
        var t = this;
        wx.showModal({
            title: "你的高级版已到期",
            content: "点击联系小名片官方客服，咨询续期继续使用",
            confirmText: "联系客服",
            showCancel: !1,
            success: function() {
                return t.gotoProvider();
            }
        });
    },
    gotoProvider: function() {
        var t = this.data.card.guid;
        (0, _radarUtil.providerService)(t).catch(function() {
            return (0, _contactUtil.gotoVersion)(t, "type=contacts&expire=1");
        });
    },
    gotoManage: function(t) {
        var a = this, e = t.currentTarget.dataset.name, r = this.data.card.guid, i = this.otherContact.hasminiapp, n = new Map([ [ "edit", function() {
            return "/pages/contacts/edit/edit";
        } ], [ "site", function() {
            return "/pages/contacts/bind/bind";
        } ], [ "person", function() {
            var t = a.data.applyCount;
            a.$route("/pages/contacts/member/member?guid=".concat(r).concat(t ? "&applylist=1" : ""));
        } ], [ "radar", function() {
            return a.openRadar();
        } ], [ "miniprogram", function() {
            return i ? "/pages/miniprogram/manage/manage?guid=".concat(r) : "/pages/miniprogram/guide/guide";
        } ], [ "card", function() {
            return a.corpCardTemp();
        } ] ]).get(e).call(this);
        "string" == typeof n && this.$route(n);
    },
    corpCardTemp: function() {
        var r = this, t = this.data.card, i = t.guid, n = t.cardstyle, o = t.isGroup, s = t.isFeeVer;
        wx.showActionSheet({
            itemList: [ "企业更多信息模板", "名片模板", "企业话术库" ],
            success: function(t) {
                var a = t.tapIndex, e = [ o ? "/pages/contacts/template/template?guid=".concat(i) : "/pages/card/more/more?iscorp=1&guid=".concat(i), "/pages/card/style/style?cardstyle=".concat(n, "&guid=").concat(i), s ? "/publicPackage/salesradar/settings/talklibrary/manage/manage" : "/publicPackage/salesradar/settings/intro/intro?guid=".concat(i) ];
                r.$route(e[a]);
            }
        });
    },
    onReport: function(t) {
        var a = t.currentTarget.dataset.report;
        a && this.$report(a);
    },
    onService: function() {
        this.$report("mine_tap_report"), (0, _radarUtil.radarService)();
    }
});